import logging
from logging.handlers import RotatingFileHandler
import time
logging.basicConfig(filename='logs/example.log', level=logging.DEBUG
                    , format='%(asctime)s - %(levelname)s - %(message)s')

# 创建 RotatingFileHandler
rotating_handler = RotatingFileHandler('logs/mylogfile.log', maxBytes=1024, backupCount=3)
rotating_handler.setLevel(logging.DEBUG)
rotating_handler.setFormatter(logging.Formatter('%(asctime)s - %(levelname)s - %(message)s'))

# 添加到日志记录器
logger = logging.getLogger()
#logger.addHandler(rotating_handler)

for i in range(100):
    logger.debug('This is a debug message')
    logger.info('This is an info message')
    logger.warning('This is a warning message')
    logger.error('This is an error message')
    logger.critical('This is a critical message')
    logger.exception('This is an exception message')
    time.sleep(1)
logging.debug('This is a debug message')
logging.info('This is an info message')
logging.warning('This is a warning message')
logging.error('This is an error message')
logging.critical('This is a critical message')
logging.exception('This is an exception message')